This document describes the difference of cryptocurrencies between several plateforms. The goal is to study wether or not some arbitrage opportunities exist, and if so where are the best options.
library(tidyverse)
library(rmarkdown) # You need this library to run this template.
library(epuRate) # Install with devtools: install_github("holtzy/epuRate", force=TRUE)
library(DT)
library(plotly)
library(hrbrthemes)
This document is based on a dataset described here and available here. Basically it report the price of 5 currencies (BTC, ETH, BCH, XRP, LTC) on 4 exchanges (Kraken, Bitstamp, Cex, Bitfinex). All currencies are not necessarily available on each plateform, as described in the table below:
# Load result
load("DATA/public_ticker_harvest.Rdata")
Ticker$last <- as.numeric(Ticker$last)
Ticker %>% nrow()
## [1] 799487
Ticker= head(Ticker, 100000)
Cryptocurrency prices evolves quickly. At each timestamp, we can have a look to the prices of Bitcoin on every platform. If you are interested by this curve, note that they are provided for every currency here.
p <- Ticker %>%
filter(symbol=="BTCEUR") %>%
ggplot( aes(x=time, y=last, color=platform, group=platform)) +
geom_line() +
theme_ipsum()
ggplotly(p)
If we zoom on a specific area, it is obvious that the price is not exactly the same on every platform.
p <- Ticker %>%
head(10000) %>%
filter(symbol=="BCHEUR") %>%
ggplot( aes(x=time, y=last, color=platform, group=platform)) +
geom_line() +
theme_ipsum()
ggplotly(p)
Can we try to quantify the difference? At each timestamp, we can calculate the difference between 2 exchanges, in euros and in %. We can then represent this difference. Here is an example with the difference between Bitstamp and Cex.
plot_last_difference <- function(plat1, plat2){
# First I calculate the differnce of the 'last' price between both exchanges:
diff <- Ticker %>%
filter(platform %in% c(plat1, plat2)) %>%
select(time, platform, last, symbol) %>%
spread(platform, last) %>%
mutate(diff=.[[4]] - .[[3]], diff_perc=(.[[4]] - .[[3]]) / .[[4]] *100 ) %>%
filter(!is.na(diff_perc))
# Then I plot the result
ggplot(diff, aes(x=time, y=diff_perc, group=symbol, fill=symbol)) +
geom_area() +
facet_wrap(~symbol, nrow=5) +
theme(legend.position="none")
}
plot_last_difference("Cex", "Bitstamp")

plot_last_difference("Kraken", "Bitstamp")

plot_last_difference("Kraken", "Bitfinex")

It’s important to note that we buy crypto at the ask price, and sell it at the bid price. For each time stamp, I can calculate the potential gain taking that into account:
# A function that calculates the difference between 2 platforms for every currency at each time stamp
find_askbid_difference <- function(plat1, plat2){
diff <- Ticker %>%
filter(platform %in% c(plat1, plat2)) %>%
select(time, platform, symbol, ask, bid) %>%
mutate(ask=as.numeric(ask), bid=as.numeric(bid)) %>%
gather(temp, value, -time, -platform, -symbol) %>%
mutate(platform=gsub(plat1,"plat1", platform)) %>%
mutate(platform=gsub(plat2,"plat2", platform)) %>%
unite(temp1, platform, temp, sep="_") %>%
spread( key=temp1, value=value) %>%
mutate(
diff1=(plat1_bid-plat2_ask)/plat1_bid*100,
diff2=(plat2_bid-plat1_ask)/plat2_bid*100
) %>%
rowwise() %>%
mutate( diff_perc=max(diff1, diff2) ) %>%
filter(!is.na(diff_perc))
return(diff)
}
plot_askbid_difference <- function(diff){
p <- ggplot(diff, aes(x=time, y=diff_perc, group=symbol, fill=symbol)) +
geom_area() +
facet_wrap(~symbol, nrow=5) +
theme(legend.position="none")
ggplotly(p)
}
# Find differences between kraken and bitstamp
diff <- find_askbid_difference("Cex", "Bitstamp")
plot_askbid_difference(diff)
# Find differences between kraken and bitstamp
diff <- find_askbid_difference("Kraken", "Bitstamp")
plot_askbid_difference(diff)